From 5789c4fbc3a06bb6840e13d42a74dc2253dfc660 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timm=20B=C3=A4der?= Date: Thu, 13 Oct 2016 19:02:47 +0200 Subject: [PATCH] tests: Add testgaction.c --- tests/Makefile.am | 1 + tests/testgaction.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 tests/testgaction.c diff --git a/tests/Makefile.am b/tests/Makefile.am index a2d3bcce1a..0b4b71f9e7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -161,6 +161,7 @@ noinst_PROGRAMS = $(TEST_PROGS) \ listmodel \ testpopup \ testpopupat \ + testgaction \ $(NULL) if USE_X11 diff --git a/tests/testgaction.c b/tests/testgaction.c new file mode 100644 index 0000000000..c9ae96ed5d --- /dev/null +++ b/tests/testgaction.c @@ -0,0 +1,148 @@ +#include + +GtkWidget *label; + + +static void +change_label_button () +{ + gtk_label_set_label (GTK_LABEL (label), "Text set from button"); +} + +static void +normal_menu_item () +{ + gtk_label_set_label (GTK_LABEL (label), "Text set from normal menu item"); +} + +static void +toggle_menu_item () +{ + + gtk_label_set_label (GTK_LABEL (label), "Text set from toggle menu item"); +} + +static void +submenu_item () +{ + gtk_label_set_label (GTK_LABEL (label), "Text set from submenu item"); +} + +static void +radio (GSimpleAction *action, GVariant *parameter, gpointer user_data) +{ + GVariant *new_state = g_variant_new_string (g_variant_get_string (parameter, NULL)); + char *str; + + str = g_strdup_printf ("From Radio menu item %s", + g_variant_get_string (new_state, NULL)); + + gtk_label_set_label (GTK_LABEL (label), str); + + g_free (str); +} + + + +static const GActionEntry win_actions[] = { + { "change-label-button", change_label_button, NULL, NULL, NULL }, + { "normal-menu-item", normal_menu_item, NULL, NULL, NULL }, + { "toggle-menu-item", toggle_menu_item, NULL, "true", NULL }, + { "submenu-item", submenu_item, NULL, NULL, NULL }, + { "radio", radio, "s", "1", NULL }, +}; + + +static const char *menu_data = + "" + " " + "
" + " " + " Normal Menu Item" + " win.normal-menu-item" + " " + " " + " Submenu" + " " + " Submenu Item" + " win.submenu-item" + " " + " " + " " + " Toggle Menu Item" + " win.toggle-menu-item" + " " + "
" + "
" + " " + " Radio 1" + " win.radio" + " 1" + " " + " " + " Radio 2" + " win.radio" + " 2" + " " + " " + " Radio 3" + " win.radio" + " 3" + " " + "
" + "
" + "
" +; + + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); + GtkWidget *menubutton = gtk_menu_button_new (); + GtkWidget *button1 = gtk_button_new_with_label ("Change Label Text"); + GtkWidget *menu; + GSimpleActionGroup *action_group; + + + action_group = g_simple_action_group_new (); + g_action_map_add_action_entries (G_ACTION_MAP (action_group), + win_actions, + G_N_ELEMENTS (win_actions), + NULL); + + gtk_widget_insert_action_group (window, "win", G_ACTION_GROUP (action_group)); + + + label = gtk_label_new ("Initial Text"); + gtk_widget_set_margin_top (label, 12); + gtk_widget_set_margin_bottom (label, 12); + gtk_container_add (GTK_CONTAINER (box), label); + gtk_widget_set_halign (menubutton, GTK_ALIGN_CENTER); + { + GMenuModel *menu_model; + GtkBuilder *builder = gtk_builder_new_from_string (menu_data, -1); + menu_model = G_MENU_MODEL (gtk_builder_get_object (builder, "menu_model")); + + menu = gtk_menu_new_from_model (menu_model); + + } + gtk_menu_button_set_popup (GTK_MENU_BUTTON (menubutton), menu); + gtk_container_add (GTK_CONTAINER (box), menubutton); + gtk_widget_set_halign (button1, GTK_ALIGN_CENTER); + gtk_actionable_set_action_name (GTK_ACTIONABLE (button1), "win.change-label-button"); + gtk_container_add (GTK_CONTAINER (box), button1); + + gtk_container_add (GTK_CONTAINER (window), box); + + + + + + + + gtk_widget_show_all (window); + gtk_main (); + return 0; +} -- 2.30.2